package com.huluobo.a2307monthdemo2.shop

import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.ToastUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.huluobo.a2307monthdemo2.adapter.CommonPagerAdapter
import com.huluobo.a2307monthdemo2.databinding.ActivityDetailBinding
import com.huluobo.lib_base.bean.CommentBean
import com.huluobo.lib_base.bean.GoodsBean
import com.huluobo.lib_base.mvvm.BaseActivity
import com.huluobo.lib_base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class DetailActivity : BaseActivity<ActivityDetailBinding, ShopViewModel>() {
    private val tabEntities = arrayListOf<CustomTabEntity>(TabEntity("详情"), TabEntity("评论"))

    private val commonPagerAdapter = CommonPagerAdapter(this, listOf(DetailFragment(), CommentFragment()))

    private lateinit var goodsBean: GoodsBean

    override fun initData() {
        vm.getGoodsDetail(intent.getIntExtra(Const.PARAM_GOODS_ID, 0))
        vm.getGoodsDetailSuccess.observe(this) {
            binding.goods = it
            goodsBean = it
        }

        vm.addToCartSuccess.observe(this) {
            ToastUtils.showLong("添加购物车成功")
        }
    }

    override fun initView() {
        binding.detailTabLayout.setTabData(tabEntities)
        binding.detailVp2.adapter = commonPagerAdapter

        binding.detailTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                binding.detailVp2.currentItem = position
            }

            override fun onTabReselect(position: Int) {
            }
        })

        binding.detailVp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.detailTabLayout.currentTab = position
            }
        })

        binding.detailAddCart.setOnClickListener {
            val map = mapOf(
                Const.PARAM_GOODS_ID to goodsBean.id,
                Const.PARAM_COUNT to 1
            )
            vm.addToCart(map)
        }

        binding.detailToCart.setOnClickListener {
            startAct(CartActivity::class.java)
        }
    }
}